import Vue from 'vue'
let vm = new Vue();
import store from '@/store'
import * as API_liveKit from '@/api/livekit'
export function checkLiveKitSingle(message) {
	let globalData = getApp().globalData
	let online = store.getters.onLine
	const callId = store.getters.callId
	console.log('checkLiveKitSingle-->',message)
	switch (message.action) {
		case '900':
			if(online) return API_liveKit.singleBusy();
			
			if (vm.$nativeRtc.isUIKitEnable() ) {
				handleSingleReceived(message);
				return;
			}
			let content = JSON.parse(message.content);
			let chatId = message.sender;
			let type = content.type == 'audio'? 'voiced':'videod';
			let roomTag = content.roomTag;
			uni.navigateTo({
			  url: '/pages/liveKitSingle/index?chatid='+chatId+ '&type='+type+'&roomTag='+ roomTag
			})
			break;
		case '902':
			console.log('对方接受了livekit单人通话');
			handleMessage(message)
			uni.$emit('liveKitAccept', message);
			break;
		case '903':
			console.log('对方拒绝了livekit单人通话');
			handleMessage(message)
			uni.$emit('liveKitSingleRegect', message);
			break;
		case '904':
			console.log('对方正忙');
			handleMessage(message)
			uni.$emit('liveKitSingleBusy', message);
			break;
		case '905':
			console.log('对方挂断');
			handleMessage(message)
			uni.$emit('liveKitSinglehangUp', message);
			break;
		case '906':
			console.log('对方取消了通话');
			handleMessage(message)
			uni.$emit('liveKitSingleCancel', message);
			break;
		case '932':
			handleMessage(message)
			console.log('对方已经响铃');
			uni.$emit('liveKitSingLing', message);
			break;
	}
}
function handleMessage(message) {
	if (vm.$nativeRtc.isUIKitEnable() ) {
		let newMessage = {
			"id": message.id,
			"sender": message.sender,
			"action": message.action,
			"content": message.content,
			"extra": message.extra,
			"timestamp": message.timestamp
		}
		vm.$nativeRtc.onMessageReceived(newMessage)
	}
}
function handleSingleReceived(message) {
	let newMessage = {
		"id": message.id,
		"sender": message.sender,
		"action": message.action,
		"content": message.content,
		"extra": message.extra,
		"timestamp": message.timestamp
	}
	vm.$nativeRtc.onSingleIncomingCall(newMessage)
}